package com.example.demo.day0303;

import java.lang.reflect.*;
public class classSelectingMethods {
    public static void main(String[] args) {

        Object obj = null;
        try {
            Class clazz = Class.forName("com.example.demo.day0303.IMP2");
            try {
                  obj = clazz.newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        if( null == obj){
            return ;
        }
     //   IMP imp =new IMP();
        InvocationHandler sm = new MethodSelector(obj);//使用接口接收代理对象
        SomeMethods proxy = (SomeMethods) Proxy.newProxyInstance(sm.getClass().getClassLoader(),obj.getClass().getInterfaces(),  sm);
        //参数1是代理对象的类加载器,参数2是被代理的对象的接口,参数3是代理对象,返回的值就成功被代理后对象,返回的是Object类型,需要强行转换
        proxy.boring1();
        proxy.boring2();
        proxy.interesting("bonobo");
        proxy.boring3();


        IT it =new IT();
        InvocationHandler itsm = new MethodSelector(it);//使用接口接收代理对象
        ITestDemo proxy1 = (ITestDemo) Proxy.newProxyInstance(itsm.getClass().getClassLoader(),it.getClass().getInterfaces(),  itsm);
        //参数1是代理对象的类加载器,参数2是被代理的对象的接口,参数3是代理对象,返回的值就成功被代理后对象,返回的是Object类型,需要强行转换
        proxy1.test1();
        proxy1.test2();

    }
}
